package com.uinnova.springboot.demo.encache;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author 张攀钦
 * @date 2018-12-25-23:05
 * ${DES}
 */
@Service
@CacheConfig(cacheNames = "ehcache_demo")
public class CacheService {

    /**
     * @author 张攀钦
     * @date 2018/12/25-23:21
     * 先查询缓存，查不到，执行方法，再将结果缓存
     * condition 方法调用前评估
     * unless 方法调用后评估，满足条件不缓存,youxianji gao
     */
    @Cacheable(key = "#id",condition = "#id.equals('1')||#id.equals('2')",unless = "#id.equals('2')")
    public String getCache(String id) {
        System.out.println("getCache");
        return id;
    }

    /**
     * @author 张攀钦
     * @date 2018/12/25-23:22
     * 删除缓存
     */
    @CacheEvict(key = "#id")
    public String cacheEvict(String id) {
        System.out.println("chacheEvict");
        return id + "删除";
    }

    /**
     * @author 张攀钦
     * @date 2018/12/25-23:22
     * 插入或更新缓存
     */
    @CachePut(key = "#id")
    public String cachePut(String id) {
        System.out.println("cachePut");
        return id + "cachePut";
    }
}
